#include <iostream>

using namespace std;

const int N = 1e5 + 10;
typedef long long LL;
LL a[N];
int n, m;

int main()
{
	cin >> n >> m;
	int maxlen = 0;
    int minlen = 0x3f3f3f3f;
    if(m == 0)
    {
        cout << 0 << endl;
        return 0;
    }
    LL ret = 0;
	for(int i = 1; i <= n; i++) 
	{
		
		int x, w; cin >> x >> w;
        minlen = min(minlen, x);
		maxlen = max(maxlen, x);
		a[x] += w;
        ret += w;
	}
    if(m >= maxlen - minlen)
    {
        cout << ret << endl;
        return 0;
    }
	for(int i = 1; i <= n; i++)
	{
		a[i] += a[i - 1];
	}
	LL ans = 0;
	for(int i = m; i + m <= maxlen; i++)
	{
		ans = max(ans, a[i] - a[i - m]);
	}
	cout << ans << endl;
	return 0;
}
